<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>点击事件</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .d1 {
            width: 80%;
            height: 200px;
            margin: 30px auto;
            border-width: 2px;
            border-color: red green blue pink;
            border-style: dotted solid double dashed;
        }

        .btn1 {
            width: 150px;
            height: 50px;
            background-color: blueviolet;
        }

        .btn2 {
            width: 150px;
            height: 50px;
            background-color: blue;
        }

        #btn3 {
            width: 200px;
            height: 50px;
            background-color: orange;
        }
        #btn4 {
            width: 150px;
            height: 50px;
            background-color: red;
        }
    </style>
    <script>
        function btn3(){
            alert("按钮3被点击了");
        }
    </script>
    <script>

        window.onload = function () {//网页加载时执行
            var btn4 = document.getElementById("btn4");
            btn4.onclick = function () {
                alert("按钮4被点击了");
            }
        }
    </script>
</head>
<body>
<div class="d1">
    <div>点击事件（4种）：</div>
    <button class="btn1">按钮1(button)监听事件</button>
    <input type="button" class="btn2" value="按钮2(input)点击事件" onclick="btn2()">
    <input type="button" id="btn3" value="按钮3(input)onclick()head内" onclick="btn3()">
    <div id="btn4">按钮4(div)head标签内</div>
</div>
<script>
    var btn1 = document.getElementsByClassName("btn1")[0]//因为class可以重复所以要标明是第几个
    var btn2 = document.getElementsByClassName("btn2")[0];
    btn1.addEventListener("click", function () {//监听事件
        alert("按钮1被点击了");
    });
    btn2.onclick = function () {//点击事件
        alert("按钮2被点击了");
    }
</script>
</body>
</html>